/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo4;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import Modelo.DAL.ConexionBD;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletOutputStream;

import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.util.JRLoader;

import Acceso.Security;

/**
 *
 * @author Jose Leonardo
 */
public class reporteAsistenciaAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String[] a = {"Administrador", "Director DSI",
            "Jefe Apoyo Administrativo", "Personal Administrativo"};
        if (!Security.validateSession(request, a)) {
            return mapping.findForward("errorPriv");
        }

        System.out.println(">>> Inicia Obtencion de Parametros");

        reporteAsistenciaForm formBean = (reporteAsistenciaForm) form;
        String nombreS = formBean.getNombreSuperv();
        String apellidoS = formBean.getApellSuperv();
        String supervisor = nombreS + " " + apellidoS;
        String descArea = formBean.getDescArea();
        String idArea = formBean.getIdArea();

        System.out.println(">>> Inicia Formateo de fecha");
        Date fechaG = formBean.getFechaG();
        String fechaGS = formBean.getFechaGstring();
        SimpleDateFormat formato = new SimpleDateFormat("dd/MM/yyyy");
        try {
            fechaG = formato.parse(fechaGS);
        } catch (Exception e) {
            System.out.println("//////////Error en el formato de la hora");
        }

        System.out.println(">>> Inicia Mapeo de parametros");
        byte[] bytes = null;

        try {
            Map parametros = new HashMap();
            parametros.put("fechaG", fechaG);
            parametros.put("area", Integer.parseInt(idArea));
            parametros.put("sede", formBean.getSede());
            parametros.put("observ", formBean.getObserv());
            parametros.put("superv", supervisor);
            parametros.put("fechastring", fechaGS);
            parametros.put("decripArea", descArea);

            System.out.println(">>> Inicia Contruccion de reporte");

            JasperReport jasperReport = (JasperReport) JRLoader.loadObject(getClass().getResource(
                    "/Modulo4/report2.jasper"));
            System.out.println("antes jasperRunManager");
            ConexionBD conn = new ConexionBD();
            bytes = JasperRunManager.runReportToPdf(jasperReport, parametros, conn.session);
            System.out.println(bytes.length);

        } catch (Exception e) {
            System.out.println("Error " + e.getMessage());
        }

        System.out.println(">>> Inicia Envio de PDF");
        if (bytes != null && bytes.length > 0) {

            response.setContentType("application/pdf");
            response.setHeader("Cache-Control", "cache");
            response.setHeader("Pragma", "cache");
            response.setHeader("Content-Disposition",
                    "attachment; filename=\"Reporte_Asistencia.pdf\"");

            response.setContentLength(bytes.length);

            ServletOutputStream outputStream = response.getOutputStream();

            outputStream.write(bytes);
            outputStream.flush();
            outputStream.close();
            return mapping.findForward(SUCCESS);
        }

        return mapping.findForward(FAILURE);
    }
}
